package com.lp.business.function;

import java.util.HashMap;
import java.util.Map;

import org.iframework.commons.util.HttpUtil;
import org.iframework.commons.util.fast.L;

import com.alibaba.fastjson.JSONObject;
import com.lp.business.conf.Conf;

/**
 * 微信相关工具包
 * @author lipeng
 *
 */
public class WXUtil {
	
	/**
	 * 根据微信code，通过微信解析出openid
	 *
	 */
	public static String getOpenIdByWXCode(String code) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appid", Conf.appid);
		params.put("secret", Conf.secret);
		params.put("grant_type", Conf.grant_type);
		params.put("js_code", code);
		String openid = null;
		try {
			//获取openID
			String result = HttpUtil.get(Conf.wxLoginUrl, params);
			JSONObject json = JSONObject.parseObject(result);
			openid = json.getString("openid");
		} catch (Exception e) {
			L.e("找不到openid,用户验证失败");
			L.e(e.getMessage(),e);
		}
		return openid;
	}

}
